<?php
require_model('base.class');
class doctor extends base {
    private $table = 'Doctors';
    private $primary = 'Id';
    function __construct() {
        parent::__construct($this->table,$this->primary);
    }

    function CreateDoctor($insert) {
        return $this->insert_item($insert);
    }
    function ModifyDoctor($update){
        $doctorname = $this->ctx->get_session('doctor_user_name', true);
        return $this->update_item($update, array('UserName'=>$doctorname));
    }
    function ApproveDoctor() {

    }
    function GetDoctorByUserName($username){
        return $this->get_item(array('UserName'=>$username));
    }
    //DoctorId是医生的电话
    function GetDoctorByDoctorId($doctorId) {
        return $this->get_item(array('DoctorId'=>$doctorId));
    }
    function GetDoctorByUserNameAndPassword($username, $password) {
        return $this->get_item(array('UserName'=>$username, 'Password'=>$password));
    }
    function get_doctor_info($user_name) {
        return $this->get_item(array('user_name'=>$user_name));
    }
    function add_doctor(array & $request , array & $response) {
        $user_name = isset($request['user_name']) ? base64_decode($request['user_name']) : '';
        $phone = isset($request['phone']) ? base64_decode($request['phone']) : '';
        $pwd = isset($request['password']) ? base64_decode($request['password']) : '';
        $cpwd = isset($request['repassword']) ? base64_decode($request['repassword']) : '';
        //手机验证码
        $check_code = isset($request['check_code']) ? base64_decode($request['check_code']) : '';
        //短信验证码 //待准备
        $checknumber = isset($request['checknumber']) ? strtolower($request['checknumber']) : '';

        //$validimg = $this->ctx->get_session('validimg_doctor_reg', true);
        //$validimg = strtolower($validimg);

        //输入的 验证码 是不是正确,防刷屏的
        /*
        if ($checknumber != $validimg || empty($validimg) || empty($checknumber)) {
            return $response['data'] = array('msg' => lang(''), 'code' => -102);
        }
        */


        if (empty($phone) || !preg_match("/^(((1[1-9]{1})|(1[1-9]{1})|(1[1-9]{1}))+\d{9})$/", $phone)) {
            return $response['data'] = array('msg' => lang('the_format_of_the_mobile_is_error'), 'code' => 4);
        }

        /*//验证手机验证码 start*/
        /*
            if(!$check_code) {
                return $response['data'] = array('msg' =>lang('checked_code_is_empty'), 'code' => 6);
            }
            $check_tel_code = $this->ctx->get_session('phone_'.$phone, true);
            $check_time = $this->ctx->get_session('phone_'.$phone.'_time', true);
            if(!(isset($check_tel_code) && isset($check_time) && $check_time-time() <= $this->phone_time * $this->phone_multiple) ) {
                return $response['data'] = array('msg' => lang('checked_code_is_gone'), 'code' => 7);
            }
            if($check_tel_code != $check_code) {
                return $response['data'] = array('msg' => lang('checked_code_is_error'), 'code' => 8);
            }
        */
        /*//验证手机验证码 end*/
        if($pwd !== $cpwd) {
            return $response['data'] = array('code'=>2, 'msg'=>lang('not_the_same_password'));
        }
        if($user_name) {
            $status = $this->get_item(array('user_name'=>$user_name));
            if($status) {
                return $response['data'] = array('code'=>0, 'msg'=>lang('user_name_is_exist'));
            }
        }
        if($phone) {
            $status = $this->get_item(array('doctor_id'=>$phone));
            if($status) {
                return $response['data'] = array('code'=>3, 'msg'=>lang('phone_is_exist'));
            }
        }
        $insert = array('user_name'=>$user_name, 'doctor_id'=>$phone , 'password'=>md5($pwd));
        $r = $this->db->create_mapper($this->table)->insert($insert, true);
        //注册是否成功
        if (!$r) {
            return $response['data'] = array('msg' =>lang('failure'), 'code' => 5);
        }
        $this->ctx->set_session('doctor_user_name', $user_name, true);
        return $response['data'] = array('msg' => lang('success'), 'code' => 1);
    }

    //医生登录
    function doctor_login(array & $request , array & $response) {
        $user_name = isset($request['user_name']) ? base64_decode( $request['user_name']) : '';
        $pwd = isset($request['password']) ? base64_decode($request['password']) : '';
        if(!$user_name) {
            return $response['data'] = array('code'=>0, 'msg'=>lang('user_name_not_allow_empty'));
        }
        if(!$pwd) {
            return $response['data'] = array('code'=>2, 'msg'=>lang('password_not_allow_empty'));
        }
        $user = $this->get_item(array('user_name'=>$user_name));
        if($user) {
            $res = $this->get_item(array('user_name'=>$user_name, 'password'=>md5($pwd)));
            if($res) {
                $this->ctx->set_session('doctor_user_name', $user_name, true);
                return $response['data'] = array('code'=>1, 'msg'=>lang('login').lang('success'));
            }else {
                return $response['data'] = array('code'=>3, 'msg'=>lang('password_is_error'));
            }
        }else {
            return $response['data'] = array('code'=>2, 'msg'=>lang('user_not_exist'));
        }
    }
    function add_apply(array & $request , array & $response) {
        /*
        $doctor_user_name = $this->ctx->get_session('doctor_user_name', true);
        $role = isset($request['role']) ? $request['role']:'';
        $server_type = isset($request['server_type']) ? $request['server_type']:'';
        if(!$role) {
            return $response['data'] = array('code'=>5,'msg'=>lang('please_select_consult_type'));
        }
        if(!$server_type) {
            return $response['data'] = array('code'=>6,'msg'=>lang('please_select_server_type'));
        }
        $role = '_'.$role;
        $server_type = '_'.$server_type;
        $res = load_model('doctorAppQu')->get_item(array('user_name'=>$$doctor_user_name));

        $insert = array(
            'departments_id'=>1,
            'user_name'=>$doctor_user_name,
            'consult_type'=>$role,
            'server_type'=>$server_type);
        $flag = load_model('doctorAppQu')->insert_item($insert);
        if($flag) {
            return $response['data'] = array('code'=>1,'msg'=>lang('success'));
        }else {
            return $response['data'] = array('code'=>0,'msg'=>lang('failure'));
        }
        */
    }
    //发送验证码
    function send_phone_check_code(array & $request, array & $response ) {
        if(isset($request['phone']) && $request['phone']){
            $phone = $request['phone'];
            $num = $this->get_item(array('doctor_id'=>$phone));
            if(count($num) == 0) {
                if($this->ctx->get_session('phone_'.$phone, true)) {
                    $prv_send_time = $this->ctx->get_session('phone_'.$phone.'_time', true);
                    if($prv_send_time- time() >= $this->phone_time * $this->phone_multiple) {
                        $domain = $this->ctx->shop_conf->get('shop_domain');
                        setcookie('phone_'.$phone.'_time', '', -86400, '/', $domain);
                        return $response['data'] = array('code'=>'-1');
                    }
                }
                /*
                $code = random(6, true);
                $this->ctx->set_session('phone_'.$phone, $code, true);
                $this->ctx->set_session('phone_'.$phone.'_time', time()+$this->phone_time*$this->phone_multiple, true);

                $msg = '51天使网提醒您：您注册的手机号码的验证码为：'.$code.'，请在注册页面填写验证码完成注册。【51天使网】';
                $responseXML = load_model('sms/sms')->sendSMS($phone,iconv('UTF-8','GB2312',$msg));
                $this->ctx->log_error('phone:'.$phone. '--->返回信息：'.$responseXML.'--->msg:'.$msg);
                $responseXML  = 1 ;
                if((int)$responseXML >= 0) {
                    $code = 1;
                } else {
                    $code = 0 ;
                }
                */
                return $response['data'] = array('code'=>isset($code) ? $code : 0, 'time'=>$this->phone_time);
            } else {
                return $response['data'] = array('code'=>'2', 'msg'=>lang('phone_is_registered'));
            }
        } else {
            return $response['data'] = array('code'=>'-1');
        }
    }
    //用户推出
    function user_logout(array & $request, array & $response, array & $app) {
        $session_id = get_session_id();
        session_destroy();
        session_unset();
        //$domain = $this->ctx->shop_conf->get('shop_domain');
        $domain = '';
        setcookie('user_name', '', -86400, '/', $domain);
        setcookie('set_key', '', -86400, '/', $domain);
        setcookie('account_info', '', -86400, '/', $domain);
        $response['data'] = array('code' => 0, 'msg' => '退出成功');
    }
    function do_app(array & $request, array & $response) {

    }
}